<template>
  <div>
    <div class="content">
      <div class="title">
        <p>订单核销码：</p>
        <span @click="handRedact">编辑</span>
      </div>
      <div class="cont">
        <input
          v-model="verificationCode"
          class="contInput"
          maxlength="6"
          type="number"
          @change="verificationChange"
          v-if="verificationState"
        >
        <input
          v-model="verificationCode"
          class="contInput"
          disabled
          v-if="!verificationState"
        >
      </div>
      <div class="hint">适用于&nbsp;“到店自取”&nbsp;订单</div>
      <div class="btn" v-if="verificationState">
        <button @click="handleClickOk" inline class="handleClickOk">确定</button>
        <button @click="handleClickCall" inline>取消</button>
      </div>
    </div>
  </div>
</template>

<script>
import { FIND_SHOP_BASE_INFO, UPDATE_SHOP_BASE_INFO } from '@/api'

export default {
  components: {},
  data () {
    return {
      verificationCode: '',
      verificationState: false
    }
  },
  created () {},
  computed: {
    account () {
      return this.$cookie.get('account')
    }
  },
  onShow () {
    this.init()
    this._showVerificationCode()
  },
  methods: {
    handRedact () {
      this.verificationState = !this.verificationState
    }, // 编辑按钮
    verificationChange (e) {
      this.verificationCode = e.mp.detail.value
    }, // 核销码change
    handleClickCall () {
      this.verificationState = false
    }, // 点击取消
    handleClickOk () {
      const vm = this
      if (vm.verificationCode === '') {
        vm.$toast('订单核销码为空~~~')
      } else {
        vm._compileRequest(vm.verificationCode)
      }
    }, // 点击确定
    _compileRequest (verificationCode) {
      const vm = this
      let params = {
        shopId: vm.account.shopId,
        extractCode: verificationCode
      }
      UPDATE_SHOP_BASE_INFO(params)
        .then(r => {
          if (r.data.code === '0' && r.data.data) {
            vm.$alert('订单核销码设置成功', '').then(() => {
              vm.$router.go(-1)
            })
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.info(e)
        })
    }, // 商户编辑店铺(设置核销码)
    _showVerificationCode () {
      const vm = this
      FIND_SHOP_BASE_INFO(vm.account.shopId).then(r => {
        if (r.data.code === '0') {
          vm.verificationCode = r.data.data.extractCode
        } else {
          vm.$toast(r.data.message)
        }
      })
    }, // 显示店铺信息(核销码)
    init () {
      this.verificationState = false
    }
  }
}
</script>

<style lang="scss">
@import 'static/styles/color.scss';

.content {
  width: calc(100% - 20px);
  margin: auto;
  height: auto;
  box-shadow: 0 0 8px lighten($blue, 20%);
  transform: translateY(10px);
  > .title {
    width: 300px;
    margin: auto;
    padding-top: 30px;
    display: flex;
    justify-content: space-between;
    font-size: 16px;
    line-height: 16px;
    color: #000;
    span {
      color: #6a93ef;
    }
  }
  > .cont {
    width: 300px;
    margin: 18px auto;
    .contInput {
      border-radius: 4px;
      border: 1px solid #e1e1e1;
      padding: 2px 15px;
      height: 36px;
      font-size: 14px;
    }
  }
  > .hint {
    width: 300px;
    margin: auto;
    padding-bottom: 18px;
    font-size: 12px;
    color: #8f8f8f;
    line-height: 12px;
  }
  > .btn {
    padding-bottom: 40px;
    display: flex;
    justify-content: space-between;
    button {
      font-size: 14px;
      width: 100px;
      height: 30px;
      line-height: 30px;
    }
    > .handleClickOk {
      background: #e51c23;
      color: #fff;
    }
  }
}
</style>
